package predict.automata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import weibo.crawler.Crawler;
import weibo4j.model.WeiboException;

import data.process.UO.UserUO;
import data.process.UO.UserUODAO;

public class Network {
	private List<UserCell> allUserCell; //元胞
	private List<String> allUserCellIdList; 
	private List<String> postedUserList;
	private List<String> toPostUserList;
	private Crawler crawler;
	UserUODAO userDao = new UserUODAO();
	private InterestItem theme; //事件主题
	
	public Network(InterestItem item){
		allUserCell = new ArrayList<UserCell>();
		allUserCellIdList = new ArrayList<String>();
		postedUserList = new ArrayList<String>();
		toPostUserList = new ArrayList<String>();
		try {
			crawler = new Crawler();
		} catch (WeiboException e) {
		} catch (IOException e) {
		}
		this.theme = item;
	}
	
	private int addUserCell(String uid) throws WeiboException {
		UserUO userUO = crawler.getUserUO(uid);
		UserCell userCell = new UserCell();
		userCell.setActiveDegree(userUO.getActiveDegree());
		userCell.setInfluence(userUO.getInfluenceDegree());
		userCell.setInterestDegree(crawler.getInterestUO(uid, theme).getInterestDegree());
		userCell.setFollowersId(crawler.getAllFollowers(uid));
		userCell.setIfPosted(false);
		userCell.setNeighbourCount(0);
		userCell.setNot_to_post(false);
		userCell.setTimeInfluenceDegree(0.1);
		userCell.setTimeWindow(0);
		userCell.setUid(uid);
		allUserCell.add(userCell);
		allUserCellIdList.add(uid);
		return allUserCell.indexOf(userCell);
	}
	
	public UserCell addPostedUserCell(String uid) throws WeiboException {
		UserCell uc = allUserCell.get(getUserCell(uid));
		uc.setIfPosted(true);
		postedUserList.add(uid);
		return uc;
	}
	
	public UserCell addToPostUserCell(String uid) {
		UserCell uc = allUserCell.get(getUserCell(uid));
		toPostUserList.add(uid);
		return uc;
	}
	
	public void removeFromToPostList(String uid) {
		this.toPostUserList.remove(this.toPostUserList.indexOf(uid));
	}
	
	public int getUserCell(String uid) {
		if (allUserCellIdList.contains(uid)) {
			return allUserCellIdList.indexOf(uid);
		}
		try {
			return this.addUserCell(uid);
		} catch (WeiboException e) {
			return -1;
		}
	}

	public List<UserCell> getAllUserCell() {
		return allUserCell;
	}

	public void setAllUserCell(List<UserCell> allUserCell) {
		this.allUserCell = allUserCell;
	}

	public List<String> getPostedUserList() {
		return postedUserList;
	}

	public void setPostedUserList(List<String> postedUserList) {
		this.postedUserList = postedUserList;
	}

	public List<String> getToPostUserList() {
		return toPostUserList;
	}

	public void setToPostUserList(List<String> toPostUserList) {
		this.toPostUserList = toPostUserList;
	}
	
}
